package com.x.erp.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.x.erp.model.ActivityUser;
import com.x.erp.rpc.ActivityUserRpcService;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "activityUser")
public class ActivityUserController extends BaseController {

	@Reference
	private ActivityUserRpcService activityUserRpcService;// 活动报名信息服务

	/**
	 * 新增报名人信息
	 *
	 * @param activityUser
	 * @return
	 */
	@RequestMapping(value = "/insertActivityUser", method = { RequestMethod.GET, RequestMethod.POST })
	public ResultModel insertActivityUser (ActivityUser activityUser){
		String activityUserMobile = activityUser.getActivityUserMobile ();
		if(StringUtils.isEmpty (activityUserMobile)){
			return new ResultModel(ResultCode.RESULT_FAIL, "报名人电话不能为空！");
		}
		if(activityUserMobile.length () != 11){
			return new ResultModel(ResultCode.RESULT_FAIL, "报名人电话长度必须为11位！");
		}
		if(StringUtils.isEmpty (activityUser.getActivityUserName ())){
			return new ResultModel(ResultCode.RESULT_FAIL, "报名人姓名不能为空！");
		}
		if(validateMobile(activityUserMobile)){
			return new ResultModel(ResultCode.RESULT_FAIL, "报名电话已存在！");
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "新增成功", activityUserRpcService.insertActivityUser (activityUser));
	}

	private boolean validateMobile(String activityUserMobile) {
		ActivityUser activityUser = new ActivityUser ();
		activityUser.setActivityUserMobile (activityUserMobile);
		activityUser = activityUserRpcService.selectActivityUser (activityUser);
		return activityUser != null;
	}

	/**
	 * 删除报名人信息
	 *
	 * @param activityUser
	 * @return
	 */
	@RequestMapping(value = "/deleteActivityUser", method = { RequestMethod.GET, RequestMethod.POST })
	public ResultModel deleteActivityUser (ActivityUser activityUser){
		return new ResultModel(ResultCode.RESULT_SUCCESS, "删除成功", activityUserRpcService.deleteActivityUser (activityUser));
	}

	/**
	 * 修改报名人信息
	 *
	 * @param activityUser
	 * @return
	 */
	@RequestMapping(value = "/updateActivityUser", method = { RequestMethod.GET, RequestMethod.POST })
	public ResultModel updateActivityUser (ActivityUser activityUser){
		String activityUserMobile = activityUser.getActivityUserMobile ();
		if(validateMobile(activityUserMobile)){
			return new ResultModel(ResultCode.RESULT_FAIL, "报名电话已存在！");
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "修改成功", activityUserRpcService.updateActivityUser (activityUser));
	}

	/**
	 * 查询报名人信息
	 *
	 * @param activityUser
	 * @return
	 */
	@RequestMapping(value = "/selectActivityUser", method = { RequestMethod.GET, RequestMethod.POST })
	public ResultModel selectActivityUser (ActivityUser activityUser){
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", activityUserRpcService.selectActivityUser (activityUser));
	}

	/**
	 * 查询报名人信息列表
	 *
	 * @param activityUser
	 * @return
	 */
	@RequestMapping(value = "/selectActivityUserList", method = { RequestMethod.GET, RequestMethod.POST })
	public ResultModel selectActivityUserList (ActivityUser activityUser){
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", activityUserRpcService.selectActivityUserList (activityUser));
	}
}
